package com.douwong.student.popuwindow;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.ColorDrawable;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.douwong.student.R;
import com.douwong.student.activity.GroupTalkActivity;


public class RecordPopu extends PopupWindow{

	private ImageView voice_img;
	private AnimationDrawable animationDrawable;
	private TextView time_text,confir_text,cancel_text;
	private RelativeLayout record_rl;
	private boolean isRecord=true;
	private int second;
	private int minute;
	private int hour;
	private View view;
	public MediaPlayer player ;
	Handler handler=new  Handler(){
		public void handleMessage(android.os.Message msg) {
			second++;
//			if(second==60){
//				second=0;
//				minute++;
//				if(minute==60){
//					minute=0;
//					hour++;
//				}
//			}
//			String time="";
//			if(hour==0&&minute==0){
//				time=second+"";
//			}
			time_text.setText(second+"");
		};
		};
	public RecordPopu(Context context, String filePath , int time){
		LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		view=inflater.from(context).inflate(R.layout.record_show, null);
		time_text=(TextView) view.findViewById(R.id.record_time);
		record_rl=(RelativeLayout) view.findViewById(R.id.record_rl);
		record_rl.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
		time_text.setText(time+"");;
		initView();
		player = new MediaPlayer();
		player.reset();
		try {
			player.setDataSource(filePath);
			player.prepare();
			player.start();
			player.setOnCompletionListener(new OnCompletionListener() {
				
				@Override
				public void onCompletion(MediaPlayer paramMediaPlayer) {
					animationDrawable=null;
					dismiss();
				}
			});
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public RecordPopu(Context context, GroupTalkActivity lister){
		LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		view=inflater.from(context).inflate(R.layout.record_popu, null);
		time_text=(TextView) view.findViewById(R.id.record_time);
		confir_text=(TextView) view.findViewById(R.id.confir_text);
		cancel_text=(TextView) view.findViewById(R.id.cancel_text);
		confir_text.setOnClickListener(lister);
		cancel_text.setOnClickListener(lister);
		initView();
		new Thread(new VoiceRecord()).start();
	}
	void initView(){
		voice_img=(ImageView) view.findViewById(R.id.record_img);
		animationDrawable = (AnimationDrawable) voice_img.getBackground();
		animationDrawable.start();
		this.setContentView(view);
		this.setWidth(LayoutParams.MATCH_PARENT);
		this.setHeight(LayoutParams.MATCH_PARENT);		
		// 设置SelectPicPopupWindow弹出窗体可点击
		this.setFocusable(true);
		ColorDrawable dw = new ColorDrawable(0xcc000000);
		this.setBackgroundDrawable(dw);
	}
	public boolean isRecord() {
		return isRecord;
	}
	public void setRecord(boolean isRecord) {
		this.isRecord = isRecord;
	}
	private class VoiceRecord implements Runnable{

		@Override
		public void run() {
			while(isRecord){
				handler.sendEmptyMessage(0);
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}
}
